(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.apTradepartner.createController', createController);
	createController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'apTradepartnerService', '$filter' ];
	function createController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, apTradepartnerService, $filter) {
		var ctrl = this, apTradepartnerInfo = $scope.$stateParams.params, errorMessage = {};
		$scope.authorization = $scope.$stateParams.authorization;
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		/**
		 * reset validate
		 * in create page only general tab, other tabs(no popup) need clear
		 */
		ctrl.validatorConfig.profBilling = {};
		ctrl.validatorConfig.profChargeback = {};
		ctrl.validatorConfig.profPayschd = {};
		ctrl.validatorConfig.profInvfilter = {};
		ctrl.apTradepartner = {};
		
		ctrl.init = function() {
			// init acct
			apTradepartnerService.initProfAcct(layoutConfig, $scope, ctrl);
			// init int
			//apTradepartnerService.initProfInt(layoutConfig, $scope, ctrl);
			//apTradepartnerService.initProfCostOfFundInt(layoutConfig, $scope, ctrl);
			// init pastdue
			apTradepartnerService.initProfPastdue(layoutConfig, $scope, ctrl);
			// init fee
			apTradepartnerService.initProfFee(layoutConfig, $scope, ctrl);
			// init doc
			apTradepartnerService.initEventDoccfg(layoutConfig, $scope, ctrl);
			// init limit facility
			//apTradepartnerService.initProfLimitFaci(layoutConfig, $scope, ctrl);
			//apTradepartnerService.initProfLimitInsFaci(layoutConfig, $scope, ctrl);
			//apTradepartnerService.initProfLimitPurFaci(layoutConfig, $scope, ctrl);
			// init Settlement Instruction
			apTradepartnerService.initProfSetlinst(layoutConfig, $scope, ctrl);
			// init cust prop define
			apTradepartnerService.initCustProp(layoutConfig, $scope, ctrl);
			//
			apTradepartnerService.initGeneral($scope, ctrl).then(function() {
				authService.create().then(function(response) {
					var initApTradepartner = response.data || apTradepartnerInfo;
					ctrl.action = "create";
					// ctrl.apTradepartner = initApTradepartner; done in initLayoutConfig
					apTradepartnerService.initLayoutConfigAndModel($scope, ctrl, initApTradepartner, ctrl.action, true).then(function() {
					});
				});
			});
		};

		ctrl.createSave = function() {
			//
			apTradepartnerService.clearDirtyData(ctrl);
			if (ctrl.apTradepartner.taskId) {
				return authService.createContinueSave(ctrl.apTradepartner).then(saveSuccessful, saveFailed);
			} else {
				return authService.createSave(ctrl.apTradepartner).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createSubmit = function() {
			apTradepartnerService.clearDirtyData(ctrl);
			if (ctrl.apTradepartner.taskId) {
				return authService.createContinueSubmit(ctrl.apTradepartner).then(submitSuccessful, submitFailed);
			} else {
				return authService.createSubmit(ctrl.apTradepartner).then(submitSuccessful, submitFailed);
			}
		};
		
		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.apTradepartner.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.aptradepartner.list',
					'state' : 'cda.apTradepartner.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
